package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
public class ArticleController {

    //使用大括号{}来写占位符
    //在@PathVariable注解的value属性中写占位符，对应的形参就可以获得占位符对应的值
    @RequestMapping("{username}/article/details/{id}")
    public BaseRespVo articleDetail(@PathVariable("username") String name,
                                    @PathVariable("id") Integer id) {

        return BaseRespVo.ok();
    }

    //localhost:8080/login?username=songge&password=niupi
    @RequestMapping("login")
    public BaseRespVo login(@RequestParam("username") String name,
                            @RequestParam("password") String pwd) {
        return BaseRespVo.ok();
    }

    //请求头的值可以使用字符串或数组来接收
    //如果以数组来进行接收：将字符串分割为数组，根据逗号来进行分割
    @RequestMapping("header")
    public BaseRespVo header(@RequestHeader("Accept") String[] accept,
                             @RequestHeader("Host") String host) {
        return BaseRespVo.ok(accept);
    }

    //通过@CookieValue的value属性获得特定name所对应的value
    @RequestMapping("cookie/value")
    public BaseRespVo cookieValue(@CookieValue("songge") String songgeValue) {
        return BaseRespVo.ok(songgeValue);
    }

    @RequestMapping("session/put/{value}")
    public BaseRespVo sessionPut(@PathVariable("value") String value, HttpSession session) {
        session.setAttribute("value",value);//attributename 👉 value
        return BaseRespVo.ok();
    }

    //在session中放入的是什么类型，就可以以什么类型取出来；
    @RequestMapping("session/fetch")
    //public BaseRespVo sessionFetch(HttpSession session) {
    //Object value = session.getAttribute("value");
    public BaseRespVo sessionFetch(@SessionAttribute("value") String value) {

        return BaseRespVo.ok(value);
    }
}
